package com.fyzk.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewTreeObserver;
import android.widget.RelativeLayout;

/**
 * Created by fanxs11 on 17/5/26.
 *
 * 跟布局(功能：软键盘弹出时界面收缩)
 */

public class ParentLayout extends RelativeLayout {

    private RelativeLayout parentLayout;
    private int softeKyboardHeight = 0;     // 软键盘高度
    private boolean isShowSofteKyboard = false; // 软键盘是否已显示

    public ParentLayout(Context context) {
        super(context);
        initView(context);
    }

    public ParentLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public ParentLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        parentLayout = this;
        parentLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

            @Override
            public void onGlobalLayout() {
//                Rect rect = new Rect();
//                // r will be populated with the coordinates of your view that area still visible.
//                parentLayout.getWindowVisibleDisplayFrame(rect);
//                int screenHeight = ScreenUtils.getInstance().getHeight();
//                int heightDiff = screenHeight - (rect.bottom - rect.top);
//                if (heightDiff > 100) {
//                    // if more than 100 pixels, its probably a keyboard
//                    // get status bar height
//                    int statusBarHeight = 0;
//                    try {
//                        Class<?> c = Class.forName("com.android.internal.R$dimen");
//                        Object obj = c.newInstance();
//                        Field field = c.getField("status_bar_height");
//                        int x = Integer.parseInt(field.get(obj).toString());
//                        statusBarHeight = getContext().getResources().getDimensionPixelSize(x);
//                    } catch (Exception e) {
//                        e.printStackTrace();
//                    }
////                    softeKyboardHeight = heightDiff - statusBarHeight;
//                } else {
//                    softeKyboardHeight = 0;
//                }
//                if (softeKyboardHeight > 0 && !isShowSofteKyboard) {
//                    LogUtil.d("软键盘height : "+ softeKyboardHeight + "屏幕height："+ ScreenUtils.getInstance().getHeight());
//                    FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ScreenUtils.getInstance().getWidth(), ScreenUtils.getInstance().getHeight() - softeKyboardHeight);
//                    parentLayout.setLayoutParams(params);
//                    isShowSofteKyboard = true;
//                } else if (softeKyboardHeight == 0 && isShowSofteKyboard) {
//                    LogUtil.d("软键盘height : "+ softeKyboardHeight + "屏幕height："+ ScreenUtils.getInstance().getHeight());
//                    FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ScreenUtils.getInstance().getWidth(), ScreenUtils.getInstance().getHeight() - softeKyboardHeight);
//                    parentLayout.setLayoutParams(params);
//                    isShowSofteKyboard = false;
//                }

            }
        });
    }
}
